import userinfo from '../../models/users/userInfo'
import ids from '../../prototype/ids'
class User extends ids{
    constructor(){
        super();
        this.addUser = this.addUser.bind(this)
    }
    async addUser(req,res,next){
        let rd = parseInt(Math.random()*3)
        let rd1 = parseInt(Math.random()*7)
        let city = ["山西","陕西","天津"]
        let date = ['2019-12-20','2019-12-19','2019-12-18','2019-12-17','2019-12-16','2019-12-15','2019-12-14']
        let no = await this.setID('user_id');
        let userInfo = new userinfo({
            city:city[rd],
            registe_time:date[rd1],
            id:Number(no),
            user_id:Number(no),
            username:'张三'
        })
        await userInfo.save()
        res.send({
            status:1
        })
    }
    async allUserList(req,res,next){
        try {
            const count=await UserModel.count()
            res.send({
                status:1,
                count
            })
        }catch (e) {
            res.send({
                status:0,
                err:'ERR_GET_COUNT_FAILED',
                count:0
            })
        }
    }
    async allUserInfo(req,res,next){
        try {
            // console.log(req.query)
            let {limit = 10,offset = 0} = req.query;
            let result = await userinfo.find({},"-_id").sort({registe_time:-1}).limit(Number(limit)).skip(Number(offset));
            res.send({
                status: 1,
                result
            })
        }catch (e) {
            res.send({
                status: 1,
                result,
                type:'ERROR_GET_ORDER_REGISTE_COUNT',
                message:'获取用户数量失败'
            })
        }
    }
    async userlist(req,res,next){
        const {limit=5,offset=0}=req.query;
        console.log(req.query);
        try {
            const users=await UserModel.find({},'-_id').sort({
                user_id:1
            }).limit(Number(limit)).skip(Number(offset));
            res.send({
                users
            })
        }catch (e) {
            res.send({
                status:0,
                type:'ERROR_GET_DATA',
                message:'用户列表获取失败'
            })
        }
    }
    async userCount(req,res,next){
        const date = req.params.date;
        // console.log(date + '=================');
        if (!date) {
            res.send({
                status: 0,
                type: 'ERROR_PARAMS',
                message: '参数错误'
            });
            return
        }
        try {
            const count = await UserModel.find({
                registe_time: eval('/^' + date + '/gi')
            });
            res.send({
                status: 1,
                count: count.length
            })
        } catch (error) {
            res.send({
                status: 0,
                type: "ERROR_GET_USER_REGISTE_COUNT",
                message: "获取当天注册人数失败"
            })
        }
    }
    // async allUser(req,res,next) {
    //     try {
    //         let count = await userinfo.count()
    //
    //     }catch (e) {
    //
    //     }
    // }
}

export default new User()